package coolq.yaozhenyong.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberIncrease;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberReduce;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;

import coolq.yaozhenyong.bean.GroupList;
import coolq.yaozhenyong.bean.QqGroupgotoRecord;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.service.GroupListService;
import coolq.yaozhenyong.service.QqGroupgotoRecordService;

@Beans
public class MyGroupGotoController {
	
	/***
	 * 	监听加群消息 并保存
	 * @param msg
	 */
	@Listen(MsgGetTypes.groupMemberIncrease)
	@Filter
	public void groupMemberIncrease(GroupMemberIncrease msg) {
		if(msg.getGroup().length()<4) {
			return ;
		}
		GroupListService Service = SpringContextUtil.getBean(GroupListService.class);
		GroupList group = Service.query().eq("groupcode", msg.getGroup()).one();
		if(group!=null) {
			String number = group.getNumber();
			int i = 2;
			try {
				i = Integer.parseInt(number);
			} catch (Exception e) {
				i = 2;
			}
			number  = (i + 1) +"";
			group.setNumber(number);
			Service.updateById(group);
		}
		/*QqGroupgotoRecordService service = SpringContextUtil.getBean(QqGroupgotoRecordService.class);
		QqGroupgotoRecord gotoentity = new QqGroupgotoRecord();
		gotoentity.setIsgoto(1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		gotoentity.setTime(df.format(new Date()));
		gotoentity.setOperqqnumber(msg.getOperatorQQ()+"同意加群");
		gotoentity.setQqnumber(msg.getBeOperatedQQ());
		gotoentity.setQunnumber(msg.getGroup());
		service.save(gotoentity);*/
	}
	
	/****
	 * 监听退群消息
	 * @param msg
	 */
	@Listen(MsgGetTypes.groupMemberReduce)
	@Filter
	public void groupMemberReduce(GroupMemberReduce msg) {
		if(msg.getGroup().length()<4) {
			return ;
		}
		GroupListService Service = SpringContextUtil.getBean(GroupListService.class);
		GroupList group = Service.query().eq("groupcode", msg.getGroup()).one();
		if(group!=null) {
			String number = group.getNumber();
			int i = 2;
			try {
				i = Integer.parseInt(number);
			} catch (Exception e) {
				i = 2;
			}
			number  = (i - 1) +"";
			group.setNumber(number);
			Service.updateById(group);
		}
		/*QqGroupgotoRecordService service = SpringContextUtil.getBean(QqGroupgotoRecordService.class);
		QqGroupgotoRecord gotoentity = new QqGroupgotoRecord();
		gotoentity.setIsgoto(0);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		gotoentity.setTime(df.format(new Date()));
		String oper = "";
		if(msg.getOperatorQQ().length()<4) {
			oper = "主动退群";
		}else {
			oper = "被"+msg.getOperatorQQ()+"移出本群";
		}
		gotoentity.setOperqqnumber(oper);
		gotoentity.setQqnumber(msg.getBeOperatedQQ());
		gotoentity.setQunnumber(msg.getGroup());
		service.save(gotoentity);*/
	}
	
	
}
